﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_MaterialOrder : UserControl
    {
        private string detail_orderno;
        private int stat;

        public panel_MaterialOrder()
        {
            InitializeComponent();
            this.stat = 0;
            this.gridMaterialOrder.BringToFront();
            MaterialOrder_LoadData();            
            this.MaterialOrder.DoubleClick += MaterialOrder_DoubleClick;
            this.MaterialOrder.PopupMenuShowing += MaterialOrder_PopupMenuShowing;
            this.materialOrderContext.ItemClicked += materialOrderContext_ItemClicked;

            this.MaterialOrderDetail.DoubleClick += MaterialOrderDetail_DoubleClick;
            this.MaterialOrderDetail.PopupMenuShowing += MaterialOrderDetail_PopupMenuShowing;
            this.materialOrderDetailContext.ItemClicked += materialOrderDetailContext_ItemClicked;
        }        

        ////////////////////////////////////////////////
        /// Material Order Details Double Click to edit
        ////////////////////////////////////////////////        
        void MaterialOrderDetail_DoubleClick(object sender, EventArgs e)
        {            
            DataRow row = MaterialOrderDetail.GetFocusedDataRow();
            frm_Edit_MaterialOrderDetail edit_form = new frm_Edit_MaterialOrderDetail();
            edit_form.detail_id = Int32.Parse(row["ID"].ToString());
            if (edit_form.LoadData())
            {
                edit_form.Show();
            }
            else
            {
                MessageBox.Show("Cannot Load data for selected order");
            }
        }

        /////////////////////////////////////////////
        /// Selection From Context Menu on Summary
        /////////////////////////////////////////////
        void materialOrderContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.materialOrderContext.Hide();
            DataRow row = MaterialOrder.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "detailsSelection":
                    this.stat = 1;
                    this.buttonSwitch.Text = "<< Summary";
                    this.labelControl1.Text = "Material Order - Details";
                    detail_orderno = row["OrderNo"].ToString();
                    MaterialOrderDetail_LoadData();
                    this.gridMaterialOrderDetail.BringToFront();
                    break;                
                case "newSelection":
                    frm_Add_MaterialOrder add_form = new frm_Add_MaterialOrder();
                    add_form.Show();
                    break;
                case "editOrderSelection":
                    frm_Edit_MaterialOrder edit_form = new frm_Edit_MaterialOrder();
                    edit_form.order_number = row["OrderNo"].ToString();
                    if (edit_form.LoadData())
                    {
                        edit_form.Show();
                    }
                    else
                    {
                        MessageBox.Show("Cannot load data for selected Order");
                    }
                    break;
                case "summaryRefreshSelection":
                    MaterialOrder_LoadData();
                    break;
            }
        }

        void MaterialOrder_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            materialOrderContext.Show(MousePosition);
        }

        void MaterialOrderDetail_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            materialOrderDetailContext.Show(MousePosition);
        }

        /////////////////////////////////////////////
        /// Selection From Context Menu on Details
        /////////////////////////////////////////////
        void materialOrderDetailContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.materialOrderDetailContext.Hide();
            DataRow row = MaterialOrderDetail.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":                    
                    frm_Edit_MaterialOrderDetail edit_form = new frm_Edit_MaterialOrderDetail();
                    edit_form.detail_id = Int32.Parse(row["ID"].ToString());
                    if (edit_form.LoadData())
                    {
                        edit_form.Show();
                    }
                    else
                    {
                        MessageBox.Show("Cannot Load data for selected order");
                    }
                    break;
                case "addSelection":
                    frm_Add_MaterialOrderDetail add_form = new frm_Add_MaterialOrderDetail();
                    if (!string.IsNullOrEmpty(detail_orderno))
                    {
                        add_form.setOrderNo(detail_orderno);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(row["OrderNo"].ToString()))
                        {
                            add_form.setOrderNo(row["OrderNo"].ToString());
                        }
                    }
                    add_form.Show();
                    break;
                case "summarySelection":
                    this.stat = 0;
                    this.labelControl1.Text = "Material Order - Summary";
                    this.buttonSwitch.Text = "Details >>";
                    this.gridMaterialOrder.BringToFront();
                    MaterialOrder_LoadData();
                    break;
                case "detailsRefreshSelection":
                    MaterialOrderDetail_LoadData();
                    break;
            }
        }


        ////////////////////////////////////////
        /// Summary double click to show details
        ////////////////////////////////////////
        void MaterialOrder_DoubleClick(object sender, EventArgs e)
        {
            this.stat = 1;
            this.labelControl1.Text = "Material Order - Details";
            this.buttonSwitch.Text = "<< Summary";
            DataRow row = MaterialOrder.GetFocusedDataRow();
            detail_orderno = row["OrderNo"].ToString();
            MaterialOrderDetail_LoadData();
            this.gridMaterialOrderDetail.BringToFront();
        }


        //////////////////////////////////////////////////
        /// Load Data for Part Mistake Summary
        //////////////////////////////////////////////////
        private void MaterialOrder_LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT OrderNo, DeadLine, OrderDate, ExpiredDate, Invoice, Remark"
                + " FROM [444_MaterialOrder]"
                + " ORDER BY OrderDate DESC, OrderNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridMaterialOrder.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("DeadLine", typeof(DateTime));
                newTable.Columns.Add("OrderDate", typeof(DateTime));
                newTable.Columns.Add("ExpiredDate", typeof(DateTime));
                newTable.Columns.Add("Invoice", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridMaterialOrder.DataSource = newTable;
            }
            MaterialOrder.Columns[0].Caption = "Order No";
            MaterialOrder.Columns[1].Caption = "Dead Line";
            MaterialOrder.Columns[2].Caption = "Order Date";
            MaterialOrder.Columns[3].Caption = "Expired Date";
            MaterialOrder.Columns[4].Caption = "Invoice";
            MaterialOrder.Columns[5].Caption = "Remark";            
            MaterialOrder.BestFitColumns();
        }

        ///////////////////////////////////////////////////
        /// Load data for Part Mistake Details
        ///////////////////////////////////////////////////
        private void MaterialOrderDetail_LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql;
            if (string.IsNullOrEmpty(detail_orderno))
            {
                sql = "SELECT [444_MaterialOrderDetail].ID, [444_MaterialOrderDetail].OrderNo,"
                    + " [444_MaterialOrder].OrderDate, [444_Material].PartNo, [444_MaterialOrderDetail].Quantity,"
                    + " [444_MaterialOrderDetail].DeadLine"
                    + " FROM [444_MaterialOrderDetail] LEFT OUTER JOIN"
                    + " [444_MaterialOrder] ON [444_MaterialOrder].OrderNo=[444_MaterialOrderDetail].OrderNo LEFT OUTER JOIN"
                    + " [444_Material] ON [444_MaterialOrderDetail].MaterialID=[444_Material].ID"
                    + " ORDER BY [444_Material].PartNo";
            }
            else
            {
                sql = "SELECT [444_MaterialOrderDetail].ID, [444_MaterialOrderDetail].OrderNo,"
                    + " [444_MaterialOrder].OrderDate, [444_Material].PartNo, [444_MaterialOrderDetail].Quantity,"
                    + " [444_MaterialOrderDetail].DeadLine"
                    + " FROM [444_MaterialOrderDetail] LEFT OUTER JOIN"
                    + " [444_MaterialOrder] ON [444_MaterialOrder].OrderNo=[444_MaterialOrderDetail].OrderNo LEFT OUTER JOIN"
                    + " [444_Material] ON [444_MaterialOrderDetail].MaterialID=[444_Material].ID"
                    + " WHERE [444_MaterialOrderDetail].OrderNo='" + detail_orderno + "'"
                    + " ORDER BY [444_Material].PartNo";
            }
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridMaterialOrderDetail.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("MaterialID", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("DeadLine", typeof(DateTime));
                gridMaterialOrderDetail.DataSource = newTable;
            }
            MaterialOrderDetail.Columns[0].Caption = "ID";
            MaterialOrderDetail.Columns[1].Caption = "Order No";
            MaterialOrderDetail.Columns[2].Caption = "Date";
            MaterialOrderDetail.Columns[3].Caption = "Material ID";
            MaterialOrderDetail.Columns[4].Caption = "Quantity";
            MaterialOrderDetail.Columns[5].Caption = "Dead Line";
            MaterialOrderDetail.Columns[0].Visible = false;
            MaterialOrderDetail.BestFitColumns();
        }

        ///////////////////////////////////////////
        /// Summary - Details Switch
        ///////////////////////////////////////////
        private void buttonSwitch_Click(object sender, EventArgs e)
        {
            switch (stat)
            {
                case 0:
                    stat = 1;
                    this.labelControl1.Text = "Material Order - Details";
                    this.buttonSwitch.Text = "<< Summary";
                    DataRow row = MaterialOrder.GetFocusedDataRow();
                    detail_orderno = row["OrderNo"].ToString();
                    MaterialOrderDetail_LoadData();
                    this.gridMaterialOrderDetail.BringToFront();                    
                    break;
                case 1:
                    stat = 0;
                    this.labelControl1.Text = "Material Order - Summary";
                    this.buttonSwitch.Text = "Details >>";
                    this.gridMaterialOrder.BringToFront();
                    MaterialOrder_LoadData();
                    break;                
            }            
        }
    }
}
